﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;

namespace ERP.ERPModels.WorkforceManage
{
    /// <summary>
    /// 排班计划
    /// </summary>
    public class SchedulingPlan : Entity<int>
    {
        /// <summary>
        /// 计划编号
        /// </summary>
        [Required]
        [StringLength(50)]
        public string PlanNumber { get; set; }

        /// <summary>
        /// 计划名称
        /// </summary>
        [Required]
        [StringLength(100)]
        public string PlanName { get; set; }

        /// <summary>
        /// 班组类型
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TeamType { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 轮班方式：白班、两班倒、三班倒
        /// </summary>
        [StringLength(20)]
        public string ShiftSystem { get; set; }

        /// <summary>
        /// 工作制：按天、按周、按月、按季度
        /// </summary>
        [StringLength(20)]
        public string WorkShiftSystem { get; set; }

        /// <summary>
        /// 倒班天数
        /// </summary>
        public int? DayCount { get; set; }

        /// <summary>
        /// 单据状态：0-草稿，1-已确认
        /// </summary>
        public int DocumentStatus { get; set; }
        
        /// <summary>
        /// 关联的班组ID列表（以逗号分隔的字符串形式存储）
        /// </summary>
        [StringLength(1000)]
        public string TeamIdsStr { get; set; } = "";
        
        /// <summary>
        /// 关联的班组ID列表
        /// </summary>
        [NotMapped]
        public List<int> TeamIds 
        { 
            get 
            {
                if (string.IsNullOrEmpty(TeamIdsStr))
                {
                    return new List<int>();
                }
                
                var result = new List<int>();
                var idStrs = TeamIdsStr.Split(',', StringSplitOptions.RemoveEmptyEntries);
                
                foreach (var idStr in idStrs)
                {
                    if (int.TryParse(idStr, out int id))
                    {
                        result.Add(id);
                    }
                }
                
                return result;
            }
            set 
            {
                if (value == null || value.Count == 0)
                {
                    TeamIdsStr = "";
                    return;
                }
                
                TeamIdsStr = string.Join(",", value);
            }
        }
    }
}
